package com.bluemsun.answerapp.service.serviceImpl;

import com.bluemsun.answerapp.controller.ExportController;
import com.bluemsun.answerapp.dao.UserDaoInterface;
import com.bluemsun.answerapp.dto.ExportExecution;
import com.bluemsun.answerapp.enums.ExportStateEnum;
import com.bluemsun.answerapp.entity.UserBean;
import com.bluemsun.answerapp.entity.data.Student;
import com.bluemsun.answerapp.service.UserService;
import com.bluemsun.answerapp.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by weizh on 2018/10/25
 * @author weizh.
 */
@Service
public class UserServiceImpl implements UserService {
    Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);


    @Autowired
    UserDaoInterface userDaoInterface;

    @Override
    public UserBean getUserByPwdAndUsername(String username, String pwd) {

        UserBean userBean=null;
        try {
            userBean=userDaoInterface.getUserByPwdAndUsernameDao(username,pwd);
        }catch (Exception ex){
            ex.getMessage();
        }

        return userBean;
    }


    /**
     * 导出学生的信息 excel
     * @param courseId
     * @return
     */
    @Override
    public ExportExecution exportStu(int courseId) {
        HSSFWorkbook workbook=null;
        ExportExecution ee=new ExportExecution();


//            FileOutputStream out = null;
//            try {
//                out = new FileOutputStream(new File("运动队负责人联系方式.xls"));
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//                logger.error(e.getMessage());
//
//            }
        String fileName = "学生名单";
        try {
            List<Student> telList = userDaoInterface.exportStu(courseId);
            workbook = ExcelUtil.getHSSFWorkbook(fileName,telList,0);
//            workbook.write(out);
//            out.close();
            System.out.println(" file written success.");
            ee.setHssfWorkbook(workbook);
            ee.setState(ExportStateEnum.SUCCESS.getState());
            ee.setStateInfo(ExportStateEnum.SUCCESS.getStateInfo());
        } catch (Exception e) {
            logger.error("导出表格出错:"+e.getMessage());
            e.printStackTrace();
            ee.setState(ExportStateEnum.INNER_ERROR.getState());
            ee.setStateInfo(ExportStateEnum.INNER_ERROR.getStateInfo());
        }

        return  ee;
    }
}
